package vip.wangwenhao.util;

import org.apache.commons.collections.CollectionUtils;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wwh
 * @date 2019年12月04日 16:47
 */

public class StringUtil {

    public static final char A = 'a';
    public static final char Z = 'z';
    public static final String UNDERLINE = "_";

    /**
     * 功能：将输入字符串的首字母改成大写
     *
     * @param str
     * @param isUp
     * @return
     */
    public static String initCap(String str, boolean isUp, List<String> prefixes) {
        if (CollectionUtils.isNotEmpty(prefixes)) {
            prefixes = prefixes.stream().sorted(Comparator.comparingInt(String::length).reversed()).collect(Collectors.toList());
            for (String prefix : prefixes) {
                if (str.startsWith(prefix)) {
                    String temp = str.replaceFirst(prefix, "");
                    if (UNDERLINE.equals(String.valueOf(temp.charAt(0)))) {
                        str = temp.substring(1);
                    }
                    break;
                }
            }

        }

        char[] ch = str.toCharArray();
        if (isUp) {
            if (ch[0] >= A && ch[0] <= Z) {
                ch[0] = (char) (ch[0] - 32);
            }
            str = new String(ch);
        }
        int i;
        while (str.indexOf(UNDERLINE) != -1) {
            i = str.indexOf(UNDERLINE);
            if (i < str.length()) {
                str = str.replaceFirst(UNDERLINE, "");
                ch = str.toCharArray();
                if (ch[i] >= A && ch[i] <= Z) {
                    ch[i] = (char) (ch[i] - 32);
                }
                str = new String(ch);
            }
        }

        return str;
    }

    public static void main(String[] args) {
        List<String> strings = Arrays.asList("r", "u", "ur");
        String ur_id = initCap("ur_id", false, strings);
        System.out.println(ur_id);
    }

}